/* Copyright 203-2115 AtomWare Technology Pty Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The flowing information must be keep 
 * ResearchRowConverter.java
 * business
 * 2013-1-4上午10:24:25
 * @author 屈甲康
 * QQ285799123
 * mailto:qujiakang@gmail.com
 */

package org.jackysoft.report.rows.converter;


import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.jackysoft.util.ExcelUtil;

import com.google.common.collect.Maps;

public class ResearchRowConverter extends AbstractRowConverter {
	
   final static Log logger = LogFactory.getLog(ResearchRowConverter.class);
   final static int[] cs = {4,7,10,13};
   int[] facs = {6,9,12,15};
   final static Map<Integer ,String> formulas = Maps.newHashMap();
   static{
	   
	   formulas.put(6, "E%d+F%d");
	   formulas.put(9, "H%d+I%d");
	   formulas.put(12, "K%d+L%d");
	   formulas.put(15, "N%d+O%d");
	   formulas.put(16, "D%d+E%d-H%d-K%d-N%d");
   }
	/**
	 * 
	 *0 - 产品代码	
	 *1 - 名称	
	 *2 - 年初	
	 *3 - 月初	
	 *4 - 当日动工    
	 *5 - 上日累计动工    
	 *6 - 累计动工	
	 *7 - 当日合格   
	 *8 - 上日累计合格    
	 *9 - 累计合格	
	 *10-   当日废品    
	 *11-  上日累计废品    
	 *12-   废品累计	
	 *13-   当日试销	   
	 *14-  上日累计试销   
	 *15-   试销累计
	 *16-   期末结存   
	 *17-   备注 
	 **/
	@Override
	public Row convert(Row oldrow) {
		//logger.info(ExcelUtil.rowString(oldrow));
		int rownum = oldrow.getRowNum()+1;
		for(int c :cs){
		   Cell cl =	oldrow.getCell(c);	
		   
		   if(cl==null)return null;
		   cl.setCellValue(0D);
		}
		
		for(int c:facs){
		   Cell p = oldrow.getCell(c-1);
		   Cell cl = oldrow.getCell(c);
		   Double clval = ExcelUtil.cellNumber(cl);
		   if(p!=null)p.setCellValue(clval);
		   
		}
		
		for(Map.Entry<Integer, String> entry:formulas.entrySet()){
		   Cell cl = oldrow.getCell(entry.getKey());
		   if(cl!=null)
		   cl.setCellFormula(String.format(entry.getValue(),rownum,rownum,rownum,rownum,rownum));
		}
		
		logger.info(ExcelUtil.rowString(oldrow));
		return (oldrow);
	}
	
}
